import type { OptionsType } from "@/components/ReSegmented";

/** 处理状态选项 */
const stateOptions: Array<OptionsType> = [
  {
    label: "待处理",
    value: 1
  },
  {
    label: "已充值",
    value: 2
  },
  {
    label: "已驳回",
    value: 3
  }
];

/** 处理状态选项（仅用于处理操作） */
const processStateOptions: Array<OptionsType> = [
  {
    label: "确认充值",
    value: 2,
    tip: "确认已完成充值操作"
  },
  {
    label: "驳回申请",
    value: 3,
    tip: "驳回此次充值申请"
  }
];

/** 获取状态标签类型 */
function getStateTagType(state: number): string {
  switch (state) {
    case 1:
      return "warning";
    case 2:
      return "success";
    case 3:
      return "danger";
    default:
      return "info";
  }
}

/** 获取状态文本 */
function getStateText(state: number): string {
  switch (state) {
    case 1:
      return "待处理";
    case 2:
      return "已充值";
    case 3:
      return "已驳回";
    default:
      return "未知";
  }
}

/** 格式化金额显示 */
function formatAmount(amount: number | string): string {
  const numAmount = typeof amount === 'string' ? parseFloat(amount) : amount;
  if (isNaN(numAmount)) return '$0.00';
  return `${numAmount.toFixed(2)}`;
}

/** 格式化时间显示 */
function formatTime(input: number | string | null | undefined): string {
  if (!input || input === 0 || input === "0") return "-";

  let date: Date;

  // 如果是字符串，先检查是否是日期格式
  if (typeof input === 'string') {
    // 检查是否是日期字符串格式（如 "2025-06-24 15:10"）
    if (input.includes('-') && (input.includes(':') || input.includes(' '))) {
      // 是日期字符串，直接解析
      date = new Date(input);
    } else {
      // 是数字字符串，当作时间戳处理
      const numTimestamp = parseFloat(input);
      if (isNaN(numTimestamp) || numTimestamp <= 0) return "-";

      if (numTimestamp.toString().length === 10) {
        date = new Date(numTimestamp * 1000);
      } else {
        date = new Date(numTimestamp);
      }
    }
  } else {
    // 是数字，当作时间戳处理
    const numTimestamp = Number(input);
    if (isNaN(numTimestamp) || numTimestamp <= 0) return "-";

    const timestampStr = numTimestamp.toString();
    if (timestampStr.length === 10) {
      date = new Date(numTimestamp * 1000);
    } else if (timestampStr.length === 13) {
      date = new Date(numTimestamp);
    } else {
      if (numTimestamp > 1000000000000) {
        date = new Date(numTimestamp);
      } else {
        date = new Date(numTimestamp * 1000);
      }
    }
  }

  // 检查日期是否有效
  if (isNaN(date.getTime())) return "-";

  return date.toLocaleString("zh-CN", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit"
  });
}

/** 判断是否可编辑 */
function canEdit(state: number): boolean {
  return state === 1; // 只有待处理状态可以编辑
}

/** 判断是否可删除 */
function canDelete(state: number): boolean {
  return state === 1; // 只有待处理状态可以删除
}

/** 判断是否可处理 */
function canProcess(state: number): boolean {
  return state === 1; // 只有待处理状态可以处理
}

/** 格式化账号信息显示（包含平台、代理商、账号） */
function formatAccountInfo(adAccount: any): string {
  if (!adAccount) return "-";

  const accountNo = adAccount.account_no || "-";
  const platform = getPlatformText(adAccount.ad_agent?.platform);
  const agentName = adAccount.ad_agent?.name || "-";

  return `${platform} | ${agentName} | ${accountNo}`;
}

/** 格式化平台和代理商信息 */
function formatPlatformAgent(adAccount: any): string {
  if (!adAccount || !adAccount.ad_agent) return "-";

  const platform = getPlatformText(adAccount.ad_agent.platform);
  const agentName = adAccount.ad_agent.name || "-";

  return `${platform} | ${agentName}`;
}

/** 获取平台文本 */
function getPlatformText(platform: number | string | null | undefined): string {
  if (!platform) return "未知平台";

  // 将字符串转换为数字
  const platformNum = typeof platform === 'string' ? parseInt(platform) : platform;

  switch (platformNum) {
    case 1:
      return "TikTok";
    case 2:
      return "Facebook";
    default:
      return "未知平台";
  }
}

export {
  stateOptions,
  processStateOptions,
  getStateTagType,
  getStateText,
  formatAmount,
  formatTime,
  canEdit,
  canDelete,
  canProcess,
  formatAccountInfo,
  formatPlatformAgent,
  getPlatformText
};
